package com.yc.pre.exception;

import com.yc.pre.consts.StatusCodeConst;
import com.yc.pre.entity.Result;
import com.yc.pre.entity.StatusCode;
import com.yc.pre.exception.custom.CodeErrorException;
import com.yc.pre.exception.custom.DataNotFoundException;
import com.yc.pre.exception.custom.DuplicateInformationException;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.MalformedJwtException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.stream.Collectors;

/**
 * @ClassName BaseExceptionHandler
 * @Description 全局异常捕捉
 * @Date 2021/02/07 16:11:52
 * @Created by ccc-j
 * @email ccc-ju@outlook.com
 */
@RestControllerAdvice
@Slf4j
public class BaseExceptionHandler {

    @ExceptionHandler(value = BadCredentialsException.class)
    public Result error(BadCredentialsException e){

        return new Result(false, StatusCode.LOGIN_ERROR, e.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    public Result error(Exception e){

        return new Result(false, StatusCode.DEFAULT_ERROR, e.getMessage());
    }

    @ExceptionHandler(value = MalformedJwtException.class)
    public Result error(MalformedJwtException e){
        return new Result(StatusCode.RE_LOGIN, "请重新登录!", null);
    }

    @ExceptionHandler(value = ExpiredJwtException.class)
    public Result error(ExpiredJwtException e){
        return new Result(StatusCode.RE_LOGIN, "请重新登录!", null);
    }


    @ExceptionHandler(value = UsernameNotFoundException.class)
    public Result error(UsernameNotFoundException e){

        return new Result(false, StatusCode.PHONE_NOT_EXIST, "账号不存在!");
    }

    @ExceptionHandler(value = CodeErrorException.class)
    public Result error(CodeErrorException e){

        return new Result(false, StatusCode.CODE_ERROR, e.getMessage());
    }

    @ExceptionHandler(value = DataNotFoundException.class)
    public Result error(DataNotFoundException e){

        return new Result(false, StatusCode.DATA_NOT_FOUND, e.getMessage());
    }

    @ExceptionHandler(value = BadSqlGrammarException.class)
    public Result error(BadSqlGrammarException e){
        log.error("SQL异常", e);
        return Result.isFail(StatusCodeConst.DEFAULT_ERROR);
    }

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public Result error(MethodArgumentNotValidException e){
        return Result.isFail(StatusCodeConst.PARAM_ERROR, e.getBindingResult()
                .getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage)
                .collect(Collectors.joining(",")));
    }

    @ExceptionHandler(value = DuplicateInformationException.class)
    public Result error(DuplicateInformationException e){

        return new Result(false, StatusCode.ACCOUNT_EXIST, e.getMessage());
    }


/*    @ExceptionHandler(value = AuthenticationException.class)
    public Result error(AuthenticationException e){

        return new Result(false, StatusCode.LOGIN_ERROR, e.getMessage());
    }*/
}
